package com.easyUtils.config;

import com.easyUtils.utils.httpClientUtil.HttpClientUtil;
import com.easyUtils.utils.httpClientUtil.properties.HttpClientProperties;
import com.easyUtils.utils.jwtUtil.JwtUtil;
import com.easyUtils.utils.jwtUtil.properties.JwtProperties;
import com.easyUtils.utils.md5Util.MD5Util;
import com.easyUtils.utils.md5Util.properties.MD5Properties;
import com.easyUtils.utils.resultUtil.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties({JwtProperties.class, HttpClientProperties.class, MD5Properties.class})
public class AutoConfiguration {

    /**
     * 创建 JWT 工具类
     * @param properties
     * @param entityClass
     * @return
     * @param <T>
     */
    @Bean
    @ConditionalOnMissingBean
    public <T> JwtUtil<T> jwtUtil(JwtProperties properties, Class<T> entityClass) {
        log.info("正在创建 JWT 工具类");
        return new JwtUtil<>(
                properties.getSecretKey(),
                properties.getExpiration(),
                entityClass
        );
    }

    /**
     * 创建 HttpClient 工具类
     * @param properties
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public HttpClientUtil httpClientUtil(HttpClientProperties properties) {
        log.info("正在创建 HttpClient 工具类");
        if (properties == null){
            return new HttpClientUtil();
        }
        return new HttpClientUtil(properties.getConnectTimeout(),properties.getResponseTimeout(),properties.getConnectionRequestTimeout());
    }

    /**
     * 创建 ResultUtil 工具类
     * @return
     */
    @Bean
    public Result<?> result() {
        log.info("正在创建 ResultUtil 工具类");
        return new Result<>();
    }

    /**
     * 创建 MD5 工具类
     * @param properties
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public MD5Util md5Util(MD5Properties properties) {
        log.info("正在创建 MD5 工具类");
        return new MD5Util(properties.getSalt());
    }
}
